﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["h"])))
            {
                string qhang = Request.QueryString["h"];

                var mahang = (from hang in da.HangSXes where hang.Tenhang == qhang select new { tenhang = hang.Mahang }).FirstOrDefault();
                string mhang = mahang.tenhang.ToString();
                
                var h = (from x in da.Sanphams where x.Hienthi == 1 && x.Mahang.Equals(mhang) orderby x.Ngaynhap descending select x).Skip(0).Take(12);
                dlsp.DataSource = h;
                dlsp.DataBind();
                if (h.Count() == 0)
                    lbloctheogia.Text = "Không có sản phẩm nào của hãng: " + qhang;
                else
                    lbloctheogia.Text = "Sản phẩm hãng: " + qhang;
            }
            else
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["key"])))
                {
                    lbloctheogia.Text = "Kết quả tìm kiếm cho từ khóa: </i>" + Request.QueryString["key"] + "</i>";
                    var search = (from y in da.Sanphams where y.Hienthi == 1 && y.Tensp.Contains(Request.QueryString["key"]) orderby y.Ngaynhap descending select y);
                    dlsp.DataSource = search;
                    dlsp.DataBind();
                }
                else
                {
                    if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["p"])))
                    {
                        string gia = Request.QueryString["p"];

                        switch (gia)
                        {
                            case "1":
                                {
                                    var search = (from y in da.Sanphams where y.Hienthi == 1 && y.Giaban>= 10000000 orderby y.Ngaynhap descending select y);
                                    lbloctheogia.Text = "Kết quả tìm kiếm theo giá trên 10.000.000 VNĐ";
                                    dlsp.DataSource = search;
                                    dlsp.DataBind();
                                    break;
                                }
                            case "2":
                                {
                                    var search = (from y in da.Sanphams where y.Hienthi == 1 && y.Giaban >=8000000 && y.Giaban <10000000 orderby y.Ngaynhap descending select y);
                                    lbloctheogia.Text = "Kết quả tìm kiếm theo giá từ 8 - 10.000.000 VNĐ";
                                    dlsp.DataSource = search;
                                    dlsp.DataBind();
                                    break;
                                }
                            case "3":
                                {
                                    var search = (from y in da.Sanphams where y.Hienthi == 1 && y.Giaban >5000000 && y.Giaban <=8000000 orderby y.Ngaynhap descending select y);
                                    lbloctheogia.Text = "Kết quả tìm kiếm theo giá từ 5 - 8.000.000 VNĐ";
                                    dlsp.DataSource = search;
                                    dlsp.DataBind();
                                    break;
                                }

                            case "4":
                                {
                                    var search = (from y in da.Sanphams where y.Hienthi == 1 && y.Giaban >2000000 && y.Giaban <=5000000 orderby y.Ngaynhap descending select y);
                                    lbloctheogia.Text = "Kết quả tìm kiếm theo giá từ 2 - 5.000.000 VNĐ";
                                    dlsp.DataSource = search;
                                    dlsp.DataBind();
                                    break;
                                }

                            case "5":
                                {
                                    var search = (from y in da.Sanphams where y.Hienthi == 1 && y.Giaban <=2000000 orderby y.Ngaynhap descending select y);
                                    lbloctheogia.Text = "Kết quả tìm kiếm theo giá dưới 2.000.000 VNĐ";
                                    dlsp.DataSource = search;
                                    dlsp.DataBind();
                                    break;
                                }

                            default:
                                break;
                        }

                    }
                    else
                    {
                        lbloctheogia.Text = "Sản phẩm mới <img src='/Online/Img/New.gif' alt='' />";
                        var a = (from x in da.Sanphams where x.Hienthi == 1 orderby x.Ngaynhap descending select x).Skip(0).Take(12);
                        dlsp.DataSource = a;
                        dlsp.DataBind();
                    }
                }
            }

        }
    }
    protected void dlsx_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlsx.SelectedIndex == 1)
        {
            var a = (from x in da.Sanphams where x.Hienthi == 1 orderby x.Giaban ascending select x).Skip(0).Take(12);
            dlsp.DataSource = a;
            dlsp.DataBind();
        }
        else
            if (dlsx.SelectedIndex == 2)
            {
                var a = (from x in da.Sanphams where x.Hienthi == 1 orderby x.Giaban descending select x).Skip(0).Take(12);
                dlsp.DataSource = a;
                dlsp.DataBind();
            }
            else
                if (dlsx.SelectedIndex == 3 || dlsx.SelectedIndex == 0)
                {
                    var a = (from x in da.Sanphams where x.Hienthi == 1 orderby x.Ngaynhap descending select x).Skip(0).Take(12);
                    dlsp.DataSource = a;
                    dlsp.DataBind();
                }
    }
}
